<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slidetype_Image
 * @category   Slide Type
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Slidetype_Image extends Bbce_Slidetype_Abstract
{
    /**
     * Title of the plugin, as shown in the interface
     * 
     * @var string
     */
    protected $_title = 'Image URL';
    
    /**
     * Description of the plugin, as shown in the interface
     * 
     * @var string
     */
    protected $_description = 'URL to an image.';
    
    /**
     * Builds the slide requested.  Returns an associative
     * array of key => value pairs of variables to be assigned
     * to the view object.
     * 
     * Example:  return array('url' => 'http://www.google.com');
     * 
     * This will assign the value of "http://www.google.com" to the view
     * variable "url".  In the view, you will access this by "echo $this->url";
     * 
     * @param int $slideId
     * @return array
     */
    public function buildSlide($slideId)
    {
        $thisSlide = $this->find($slideId);
        
        return array(
            'url'  => $thisSlide['url'],
            'bgColor'   => $thisSlide['bgColor'],
            'alignment' => $thisSlide['alignment'],
            'scaling'   => $thisSlide['scaling'],
        );
    }
    
    /**
     * Generates a thumbnail for the given slide.
     * 
     * @param $slideId
     * @return PHPGD image object
     */
    public function generateSlideThumbnail($slideId)
    {
        $slide = $this->find($slideId);
        
        $src = file_get_contents($slide['url']);
        
        return imagecreatefromstring($src);
    }       
    
    /**
     * Generates a Zend_Form object which is displayed when
     * editing the given slide type.  The $values array is
     * a key => value associative array that maps the form
     * field names with their default values, if any.
     * 
     * @param array $values
     * @return object Zend_Form
     */
    public function getConfigurationForm($values = array())
    {
        $form = new Zend_Form();
        $form->setAttrib('class', 'slideTypeForm')
             ->setDecorators(array(
                     'FormElements',
                     array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form')),
                     'Form'
             ));
             
        $url = $form->createElement('text', 'url', array('label' => 'Image Url:'));
        $url->setValue(isset($values['url']) ? $values['url'] : '');
        
        $bgColor = $form->createElement('text', 'bgColor', array('label' => 'Background Color:'));
        $bgColor->setAttrib('size', 6)
                ->setAttrib('maxlength', 6)
                ->setAttrib('class', 'color');
        $bgColor->setValue(isset($values['bgColor']) ? $values['bgColor'] : '');

        $alignmentValues = array(
                                'topLeft'      => 'Top Left',
                                'topCenter'    => 'Top Center',
                                'topRight'     => 'Top Right',
                                'centerLeft'   => 'Center Left',
                                'centerCenter' => 'Center Center',
                                'centerRight'  => 'Center Right',
                                'bottomLeft'   => 'Bottom Left',
                                'bottomCenter' => 'Bottom Center',
                                'bottomRight'  => 'Bottom Right'
                            );
        
        $alignment = $form->createElement('select', 'alignment', array('label' => 'Alignment:'));
        $alignment->setMultiOptions($alignmentValues)
                  ->setValue(isset($values['alignment']) ? $values['alignment'] : 'centerCenter');
                  
        $scalingValues = array(
                            'none' => 'No Scaling',
                            'up'   => 'Scale Up',
                            'down' => 'Scale Down',
                            'fill' => 'Fill Screen'
                         );
        
        $scaling = $form->createElement('select', 'scaling', array('label' => 'Scaling:'));
        $scaling->setMultiOptions($scalingValues)
                ->setValue(isset($values['scaling']) ? $values['scaling'] : 'down'); 
        
        $form->addElements(array($url, $bgColor, $alignment, $scaling));

        $form->setElementDecorators(array(
                  'ViewHelper',
                  'Errors',
                  array('HtmlTag', array('tag' => 'div', 'class' => 'elm')),
                  array('Label', array('tag' => 'span')),
              ));
        
        return $form;     
    }
}